﻿using BizTalk.ESB.Extensions.SSO.Data;
using System;
using System.Collections.Specialized;
using System.Diagnostics;

namespace BizTalk.ESB.Extensions.SSO.BizTalk
{
    public class SsoConfigWrapper
    {
        public static object TryGetConfigValue(string ApplicationName, string ConfigName, object DefaultReturnValue)
        {
            object result = DefaultReturnValue;
            try
            {
                var ssoConfigStore = new SsoConfigStore();
                if (ssoConfigStore.AppExists(ApplicationName))
                {
                    result = ssoConfigStore.ReadKey(ApplicationName, ConfigName);
                }
            }
            catch (MissingFieldException mfe)
            {
                Trace.WriteLine(mfe.Message);
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message);
                result = null;
            }
            return result;
        }

        public static HybridDictionary GetConfigSet(string ApplicationName)
        {
            var ssoConfigStore = new SsoConfigStore();
            var hd = new HybridDictionary();
            try
            {
                var ssoApp = ssoConfigStore.GetSsoApplication(ApplicationName);
                foreach (var p in ssoApp.PropertyBag)
                {
                    hd.Add(p.ConfigurationKey, p.ConfigurationValue);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Error retrieving configuration for SSO Configuration Application: "
                    + ApplicationName, ex);
            }
            return hd;
        }
    }
}
